package querysrc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;


public class query_current_user_id extends query{
	
	public query_current_user_id(sqlConnectionHandler sqlconhandler)
	{
		super(sqlconhandler);
	}
	
	private int getCurrentid(Connection con,String sql)
	{
		querydb qdb = null;
		ResultSet rs = null;		
		int iret = -1;
		try
		{
			boolean bret = true;				
			if(bret)
			{
				qdb = new querydb(con,sql);
				bret = qdb.prePS();
			}
			if(bret)
			{
				rs = qdb.execPSQuery();
				if (!rs.next()){
					iret = 0;
			        //secure zfp ???				
			      }
			      else {    	  
			    	  iret = rs.getInt(sqlscripts.sql_get_max_user_id_c1);			        	        
			        }
			}
			return iret;
			
	    }
	
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		finally
		{
			this.release_resultset(rs);
			this.release_querydb(qdb);			
		}
	}
	public int getCurrentUid()
	{		
		Connection con = null;
		int iret = -1;
		try
		{
			boolean bret = true;
			con = this.init();	
			int uid = this.getCurrentid(con, sqlscripts.sql_get_max_user_id);
			int vid = this.getCurrentid(con, sqlscripts.sql_get_max_visitor_id);
			iret = Math.max(vid, uid);
			iret = Math.max(iret, 0);
			return iret;
			
	    }	
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		finally
		{			
			this.release_connection(con);
			this.finalize();
		}
			
	}
	

}
